#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import JTK.jmaya.core as jmc
    import maya.mel as mel

except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class UnsedSGNode(CheckBase):
    def __init__(self):
        super(UnsedSGNode, self).__init__()
        pass

    def check(self):
        sg_list = cmds.ls(type='shadingEngine')
        self.result_proc = []
        for s in sg_list:
            if pm.PyNode(s).isReferenced():
                continue
            if mel.eval('shadingGroupUnused %s' % s) == 1:
                self.result_proc.append(s)
        if self.result_proc:
            result_log = u'Error:%s' % self.result_proc
        else:
            result_log = u'Succeed:没有问题'

        return result_log

    def fix(self):
        self.check()
        if self.result_proc:
            try:
                for i in self.result_proc:
                    if cmds.lockNode(i, q=1, l=1) == [True]:
                        cmds.lockNode(i, l=0)
                    cmds.delete(i)
                result_log = u'Succeed:%s已经删除' % self.result_proc
                return result_log
            except:
                return u'Error:%s没有删除成功' % self.result_proc
